package io.renren.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.shop.entity.GoodsCoupon;
import io.renren.modules.shop.entity.UserAndCoupon;
import io.renren.modules.shop.service.GoodsCouponService;
import io.renren.modules.shop.service.UserAndCouponService;
import io.renren.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("goods_coupon")
@Api(value = "商品优惠券Controller", tags = {"优惠券接口"})
public class GoodsCouponController extends AbstractController {
    @Autowired
    private GoodsCouponService goodsCouponService;
    @Autowired
    private UserAndCouponService userAndCouponService;

    @ApiOperation(value = "查询分页")
    @PostMapping("page")
    public R getPage(@ApiParam("优惠券实体类")@RequestBody GoodsCoupon goodsCoupon, @RequestParam Integer size, @RequestParam Integer page) {
        IPage<GoodsCoupon> iPage = new Page<>(page,size);
        IPage<GoodsCoupon> goodsCouponIPage = goodsCouponService.page(iPage, new QueryWrapper<>(goodsCoupon));
        PageUtils pageResult = new PageUtils(goodsCouponIPage);
        return R.ok().put("page",pageResult);
    }


    @ApiOperation(value = "新增优惠券")
    @PostMapping("add")
    public R addNew(@ApiParam("优惠券实体类") @RequestBody GoodsCoupon goodsCoupon) {
        boolean flag = goodsCouponService.save(goodsCoupon);
        return R.ok().put("flag",flag);
    }

    @ApiOperation(value = "根据条件查询优惠券列表")
    @PostMapping("list")
    public R getList(@ApiParam("优惠券实体类(可为空，查询所有)")@RequestBody GoodsCoupon goodsCoupon) {
        List<GoodsCoupon> list = goodsCouponService.list(new QueryWrapper<>(goodsCoupon));
        UserAndCoupon userAndCoupon = new UserAndCoupon();
        return R.ok().put("list",list);
    }

    @ApiOperation(value = "根据ids删除记录")
    @PostMapping("delete")
    public R del(@RequestBody Integer[] ids) {
        for (Integer id : ids) {
            goodsCouponService.removeById(id);
        }
        return R.ok();
    }

    @ApiOperation(value = "获取当前用户的优惠券")
    @GetMapping("getUserCoupon")
    public R getUserCoupon() {
        List<GoodsCoupon> list = goodsCouponService.getUserCoupon(getWxUser().getOpenid());
        return R.ok().put("list", list);
    }
}
